<?php
class PostController extends AppController {
    var $helpers = array ('Html','Form','Session');
    var $name = 'Post'; // riferimento alla cartella delle view
    //var $modelClass = 'Multa';
    
    public function beforeFilter(){
    	
		parent::beforeFilter();
    	$this->loadModel('Post');
		$this->Auth->allow('view');
		
    }

    public function index($category = null) {
    	$this->set("posts",$this->Post->find("all"));
    }

	public function save() {
        $this->Post->set($this->request->data);
        if($this->Post->save()){
            $this->Session->setFlash("Post aggiornato con successo");
            $this->redirect("/Post/edit/".$this->Post->id);
        } else {
            $this->redirect($this->referer());
        }
	}

	public function add() {
	   $this->layout="admin";
	   $this->set("breadcrumb",__("Aggiungi Post"));
	}
	
    public function edit($id){
	   $this->layout="admin";
       $bm = $this->Post->read(null,$id);
       $this->set("post",$bm); 
	   $this->set("breadcrumb",__("Modifica Post"));
    }
	
	public function delete($id) {
        $this->Post->delete($id);
        $this->Session->setFlash("Post eliminato con successo");
		$this->redirect("/Post");
	}
	
	public function view($title) {
        $post = $this->Post->find("first",array("conditions"=>array("Post.status"=>POST_STATUS_PUBLISH,"PostLang.title_slug"=>$title)));
		$this->set("post",$post);
	}
	
	public function revision($id) {
        $this->Post->read(null,$id);
        $this->Post->set("status",POST_STATUS_REVISION);
        if($this->Post->save()){
            $this->Session->setFlash("Post da revisionare!");
        } else {
            $this->Session->setFlash("Errore in fase di salvataggio!");
        }
        $this->redirect($this->referer());
	}

	public function publish($id) {
        $this->Post->read(null,$id);
        $this->Post->set("status",POST_STATUS_PUBLISH);
        if($this->Post->save()){
            $this->Session->setFlash("Post pubblicato!");
        } else {
            $this->Session->setFlash("Errore in fase di salvataggio!");
        }
        $this->redirect($this->referer());
	}
	
	
	
}
?>